package com.wb.exception

import com.wb.component.Slf4j
import com.wb.component.Slf4j.Companion.log
import com.wb.utils.Status
import org.springframework.validation.BindException
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestControllerAdvice
import java.lang.Exception
import java.lang.NumberFormatException
import javax.servlet.http.HttpServletRequest

/**
 * 自定义异常处理
 *
 * @author 祁雪
 */
@Slf4j
@RestControllerAdvice
@ControllerAdvice
open class LseException {

    @ResponseBody
    @ExceptionHandler
    fun exp(request: HttpServletRequest, e: NumberFormatException): Status? {
//        LogFactory.getLog(super.getClass()).error(e.getMessage(), e);
        log.error(request.servletPath, e)
        return Status.error("Please type int", e.message)
    }

    @ExceptionHandler
    @ResponseBody
    fun exp(request: HttpServletRequest, e: BindException): Status {
//        e.printStackTrace();
        log.error(request.servletPath, e)
        return Status.error(e.bindingResult.fieldError!!.defaultMessage)
    }

    @ResponseBody
    @ExceptionHandler
    fun exp(request: HttpServletRequest, e: Exception): Status {
//        LogFactory.getLog(super.getClass()).error(e.getMessage(), e);
        log.error(request.servletPath, e)
        return Status.error(e.message)
    }

}